/**
 * Course Project for User Interface Programming I, 2011
 * Group 19
 */
package justdoit.view;

import java.util.Comparator;
import justdoit.view.model.PriorityWithIcon;

/**
 * A comparator for sorting in Table
 * @author Can
 */
public class ComparatorForTable implements Comparator {

    protected boolean isSortAsc;

    public ComparatorForTable(boolean isSortAsc) {
        this.isSortAsc = isSortAsc;
    }

    public int compare(Object o1, Object o2) {
        if (o1 instanceof PriorityWithIcon && o2 instanceof PriorityWithIcon) {
            PriorityWithIcon p1 = (PriorityWithIcon) o1;
            PriorityWithIcon p2 = (PriorityWithIcon) o2;
            int result = p1.compareTo(p2);
            if (!isSortAsc) {
                result = -result;
            }
            return result;
        }
        if (o1 instanceof String && o2 instanceof String) {
            String s1 = (String) o1;
            String s2 = (String) o2;
            int result = s1.compareToIgnoreCase(s2);
            if (!isSortAsc) {
                result = -result;
            }
            return result;
        }
        if (o1 instanceof Integer && o2 instanceof Integer) {
            Integer i1 = (Integer) o1;
            Integer i2 = (Integer) o2;
            int result = i1.compareTo(i2);
            if (!isSortAsc) {
                result = -result;
            }
            return result;
        }
        System.out.println("Unexpected compare!");
        return o1.toString().compareTo(o2.toString());
    }
}
